package com.example.ymm.modules1.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.DbCategory;
import com.example.ymm.modules1.mapper.my.user.DbCategoryMapper;
import com.example.ymm.modules1.query.DbCategoryQueryCriteria;
import com.example.ymm.modules1.service.user.DbCategoryService;
import com.example.ymm.util.QueryHelpPlus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DbCategoryServiceImpl extends BaseServiceImpl<DbCategoryMapper, DbCategory> implements DbCategoryService {

    @Override
    public TableList<DbCategory> list(DbCategoryQueryCriteria criteria, YmPageDto pageDto) {
        IPage<DbCategory> page = new Page<DbCategory>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DbCategory.class, criteria);
        predicate.orderByAsc("sort");
        baseMapper.selectPage(page, predicate);
        TableList<DbCategory> tableList=new TableList(page);
        return tableList;
    }

    public List<DbCategory> queryAll(DbCategoryQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DbCategory.class, criteria);
        predicate.orderByAsc("sort");
        List<DbCategory> result =baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public DbCategory findByCategoryName(String categoryName) {
        DbCategoryQueryCriteria criteria=new DbCategoryQueryCriteria();
        criteria.setCategoryName(categoryName);
        List<DbCategory> list = queryAll(criteria);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }
}
